package cn.taketoday.live.component.impl;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.HashSet;
import java.util.Map;

import cn.taketoday.live.component.ThreadComponent;
import cn.taketoday.live.config.CenterSetConfig;
import cn.taketoday.live.config.GlobalData;
import cn.taketoday.live.config.settings.AutoReplySetConf;
import cn.taketoday.live.config.settings.AutoReplySetting;
import cn.taketoday.live.config.settings.ThankGiftRuleSet;
import cn.taketoday.live.config.settings.ThankGiftSetConf;
import cn.taketoday.live.data.enums.ShieldMessage;
import cn.taketoday.live.handler.BarrageHandler;
import cn.taketoday.live.handler.MessageHandler;
import cn.taketoday.live.http.HttpOtherData;
import cn.taketoday.live.thread.AdvertThread;
import cn.taketoday.live.thread.GiftShieldThread;
import cn.taketoday.live.thread.LogThread;
import cn.taketoday.live.thread.ParseThankGiftThread;
import cn.taketoday.live.thread.WelcomeShieldThread;
import cn.taketoday.live.thread.core.HeartByteThread;
import cn.taketoday.live.thread.online.HeartBeatThread;
import cn.taketoday.live.thread.online.HeartBeatsThread;
import cn.taketoday.live.thread.online.SmallHeartThread;
import cn.taketoday.live.thread.online.UserOnlineHeartThread;
import cn.taketoday.live.tools.ParseSetStatusTools;

/**
 * @author BanqiJane
 * @author TODAY
 */
@Component
public class ThreadComponentImpl implements ThreadComponent {

  @Override
  public void configureMessageHandler(
          Map<ShieldMessage, Boolean> messageControlMap, CenterSetConfig centerSetConf) {

    HashSet<ThankGiftRuleSet> thankGiftRuleSets = new HashSet<>();
    // thankGiftRuleSets
    final ThankGiftSetConf thankGift = centerSetConf.getThank_gift();
    if (thankGift != null) {

      final HashSet<ThankGiftRuleSet> thankGiftRuleSets1 = thankGift.getThankGiftRuleSets();
      if (!CollectionUtils.isEmpty(thankGiftRuleSets1)) {
        for (final ThankGiftRuleSet thankGiftRuleSet : thankGiftRuleSets1) {
          if (thankGiftRuleSet.isIs_open()) {
            thankGiftRuleSets.add(thankGiftRuleSet);
          }
        }
      }
    }
    final MessageHandler messageHandler = MessageHandler.getInstance();
    if (messageHandler.isEnable()) {
      GlobalData.centerSetConf = centerSetConf;
      messageHandler.setMessageControlMap(messageControlMap);
      messageHandler.setThankGiftSetConf(centerSetConf.getThank_gift());
      messageHandler.setThankGiftRuleSets(thankGiftRuleSets);
    }
    else {
      messageHandler.setEnable(true);
      messageHandler.setMessageControlMap(messageControlMap);
      messageHandler.setThankGiftSetConf(centerSetConf.getThank_gift());
      messageHandler.setThankGiftRuleSets(thankGiftRuleSets);
    }
  }

  @Override
  public void startHeartByteThread() {
    if (GlobalData.heartByteThread != null) {
      return;
    }
    GlobalData.heartByteThread = new HeartByteThread();
    GlobalData.heartByteThread.HFLAG = false;
    GlobalData.heartByteThread.start();
    if (GlobalData.heartByteThread != null
            && !GlobalData.heartByteThread.getState().toString().equals("TERMINATED")) {
    }
  }

  @Override
  public void startLogThread() {

    if (GlobalData.logThread != null) {
      return;
    }
    GlobalData.logThread = new LogThread();
    GlobalData.logThread.FLAG = false;
    GlobalData.logThread.start();
    if (GlobalData.logThread != null && !GlobalData.logThread.getState().toString().equals("TERMINATED")) {
    }
  }

  @Override
  public void startAdvertThread(CenterSetConfig centerSetConf) {

    if (GlobalData.advertThread != null || StringUtils.isEmpty(GlobalData.USERCOOKIE)) {
      GlobalData.advertThread
              .setAdvertStatus(ParseSetStatusTools.getAdvertStatus(centerSetConf.getAdvert().getStatus()));
      GlobalData.advertThread.setTime(centerSetConf.getAdvert().getTime());
      GlobalData.advertThread.setAdvertBarrage(centerSetConf.getAdvert().getAdverts());
      return;
    }
    GlobalData.advertThread = new AdvertThread();
    GlobalData.advertThread.FLAG = false;
    GlobalData.advertThread
            .setAdvertStatus(ParseSetStatusTools.getAdvertStatus(centerSetConf.getAdvert().getStatus()));
    GlobalData.advertThread.setTime(centerSetConf.getAdvert().getTime());
    GlobalData.advertThread.setAdvertBarrage(centerSetConf.getAdvert().getAdverts());
    GlobalData.advertThread.start();
    startSendBarrageThread();
    if (GlobalData.advertThread != null
            && !GlobalData.advertThread.getState().toString().equals("TERMINATED")) {
    }
  }

  @Override
  public void startAutoReplyThread(CenterSetConfig centerSetConf) {

    HashSet<AutoReplySetting> autoReplySets = new HashSet<>();
    final AutoReplySetConf reply = centerSetConf.getReply();
    final HashSet<AutoReplySetting> autoReplySets1 = reply.getAutoReplySets();
    for (final AutoReplySetting autoReplySet : autoReplySets1) {
      if (autoReplySet.isEnable()) {
        autoReplySets.add(autoReplySet);
      }
    }

    //
    BarrageHandler.setAutoReplyConfig(autoReplySets);
  }

  @Override
  public void startSendBarrageThread() {
    if (!StringUtils.isEmpty(GlobalData.USERCOOKIE)) {
      BarrageHandler.setClose(false);
    }
  }

  @Override
  public void startUserOnlineThread() {
    if (GlobalData.heartBeatThread != null || GlobalData.heartBeatsThread != null
            || GlobalData.userOnlineHeartThread != null) {
      return;
    }
    GlobalData.heartBeatThread = new HeartBeatThread();
    GlobalData.heartBeatThread.FLAG = false;
    GlobalData.heartBeatThread.start();

    GlobalData.heartBeatsThread = new HeartBeatsThread();
    GlobalData.heartBeatsThread.FLAG = false;
    GlobalData.heartBeatsThread.start();

    GlobalData.userOnlineHeartThread = new UserOnlineHeartThread();
    GlobalData.userOnlineHeartThread.FLAG = false;
    GlobalData.userOnlineHeartThread.start();

    if (GlobalData.heartBeatThread != null && GlobalData.heartBeatsThread != null
            && GlobalData.userOnlineHeartThread != null
            && !GlobalData.heartBeatThread.getState().toString().equals("TERMINATED")
            && !GlobalData.heartBeatsThread.getState().toString().equals("TERMINATED")
            && !GlobalData.userOnlineHeartThread.getState().toString().equals("TERMINATED")) {
    }
    else {
      closeUserOnlineThread();
    }
  }

  @Override
  public void startSmallHeartThread() {

    if (GlobalData.smallHeartThread != null
            && !GlobalData.smallHeartThread.getState().toString().equals("TERMINATED")) {
      return;
    }
    if (null == GlobalData.userOnlineHeartThread) {
      return;
    }
    GlobalData.SMALLHEART_ADRESS = HttpOtherData.httpPostEncsUrl();
    GlobalData.smallHeartThread = new SmallHeartThread();
    GlobalData.smallHeartThread.FLAG = false;
    GlobalData.smallHeartThread.start();
    if (GlobalData.smallHeartThread != null
            && !GlobalData.smallHeartThread.getState().toString().equals("TERMINATED")) {
    }
  }

  @Override
  public void startGiftShieldThread(String giftName, int time) {

    if (GlobalData.parsethankGiftThread.getState().toString().equals("TERMINATED")
            || GlobalData.parsethankGiftThread.getState().toString().equals("NEW")) {
      GlobalData.giftShieldThread = new GiftShieldThread();
      GlobalData.giftShieldThread.FLAG = false;
      GlobalData.giftShieldThread.setGiftName(giftName);
      GlobalData.giftShieldThread.setTime(time);
      GlobalData.giftShieldThread.start();
    }
  }

  @Override
  public void startWelcomeShieldThread(int time) {
    GlobalData.welcomeShieldThread = new WelcomeShieldThread();
    GlobalData.welcomeShieldThread.FLAG = false;
    GlobalData.welcomeShieldThread.setTime(time);
    GlobalData.welcomeShieldThread.start();
  }

  @Override
  public void closeUserOnlineThread() {

    if (GlobalData.userOnlineHeartThread != null) {
      GlobalData.userOnlineHeartThread.FLAG = true;
      GlobalData.userOnlineHeartThread.interrupt();
      GlobalData.userOnlineHeartThread = null;
    }
    if (GlobalData.heartBeatThread != null) {
      GlobalData.heartBeatThread.FLAG = true;
      GlobalData.heartBeatThread.interrupt();
      GlobalData.heartBeatThread = null;
    }
    if (GlobalData.heartBeatsThread != null) {
      GlobalData.heartBeatsThread.FLAG = true;
      GlobalData.heartBeatsThread.interrupt();
      GlobalData.heartBeatsThread = null;
    }
  }

  @Override
  public void startParseThankGiftThread(ThankGiftSetConf thankGiftSetConf,
                                        HashSet<ThankGiftRuleSet> thankGiftRuleSets) {

    if (GlobalData.parsethankGiftThread == null) {
      GlobalData.parsethankGiftThread = new ParseThankGiftThread();
    }

    final Thread.State state = GlobalData.parsethankGiftThread.getState();
    if (state == Thread.State.TERMINATED || state == Thread.State.NEW) {
      GlobalData.parsethankGiftThread = new ParseThankGiftThread();
      GlobalData.parsethankGiftThread.setDelaytime((long) (1000 * thankGiftSetConf.getDelaytime()));
      GlobalData.parsethankGiftThread.start();
      GlobalData.parsethankGiftThread.setTimestamp(System.currentTimeMillis());
      GlobalData.parsethankGiftThread.setThankGiftString(thankGiftSetConf.getThank());
      GlobalData.parsethankGiftThread
              .setThankGiftStatus(ParseSetStatusTools.getThankGiftStatus(thankGiftSetConf.getThank_status()));
      GlobalData.parsethankGiftThread.setThankGiftRuleSets(thankGiftRuleSets);
      GlobalData.parsethankGiftThread.setNum(thankGiftSetConf.getNum());
      GlobalData.parsethankGiftThread.setIs_num(thankGiftSetConf.isIs_num());
    }
    else {
      GlobalData.parsethankGiftThread.setTimestamp(System.currentTimeMillis());
      GlobalData.parsethankGiftThread.setThankGiftString(thankGiftSetConf.getThank());
      GlobalData.parsethankGiftThread
              .setThankGiftStatus(ParseSetStatusTools.getThankGiftStatus(thankGiftSetConf.getThank_status()));
      GlobalData.parsethankGiftThread.setThankGiftRuleSets(thankGiftRuleSets);
      GlobalData.parsethankGiftThread.setNum(thankGiftSetConf.getNum());
      GlobalData.parsethankGiftThread.setIs_num(thankGiftSetConf.isIs_num());
    }
  }

  @Override
  public void setAdvertThread(CenterSetConfig centerSetConf) {
    if (GlobalData.advertThread != null) {
      GlobalData.advertThread
              .setAdvertStatus(ParseSetStatusTools.getAdvertStatus(centerSetConf.getAdvert().getStatus()));
      GlobalData.advertThread.setTime(centerSetConf.getAdvert().getTime());
      GlobalData.advertThread.setAdvertBarrage(centerSetConf.getAdvert().getAdverts());
    }
  }

  @Override
  public void setAutoReplyThread(CenterSetConfig centerSetConf) {
    HashSet<AutoReplySetting> autoReplySets = new HashSet<>();
    final AutoReplySetConf reply = centerSetConf.getReply();
    if (reply != null) {
      final HashSet<AutoReplySetting> autoReplySets1 = reply.getAutoReplySets();
      if (!CollectionUtils.isEmpty(autoReplySets1)) {
        for (final AutoReplySetting autoReplySetting : autoReplySets1) {
          if (autoReplySetting.isEnable()) {
            autoReplySets.add(autoReplySetting);
          }
        }
      }
    }

    BarrageHandler.setAutoReplyConfig(autoReplySets);
  }

  @Override
  public void disableMessageHandler() {
    final MessageHandler messageHandler = MessageHandler.getInstance();
    messageHandler.setEnable(false);
  }

  @Override
  public void closeHeartByteThread() {
    if (GlobalData.heartByteThread != null) {
      GlobalData.heartByteThread.HFLAG = true;
      GlobalData.heartByteThread.interrupt();
      GlobalData.heartByteThread = null;
    }
  }

  @Override
  public void closeLogThread() {

    if (GlobalData.logThread != null) {
      GlobalData.logThread.FLAG = true;
      GlobalData.logThread.interrupt();
      GlobalData.logThread = null;
    }
  }

  @Override
  public void closeAdvertThread() {

    if (GlobalData.advertThread != null) {
      GlobalData.advertThread.FLAG = true;
      GlobalData.advertThread.interrupt();
      GlobalData.advertThread = null;
    }
    if (GlobalData.ROOM_ID != null) {
      if (!GlobalData.centerSetConf.getFollow().isIs_open()
              && !GlobalData.centerSetConf.getThank_gift().isIs_open()) {
        closeSendBarrageThread();
      }
    }
    else {
      closeSendBarrageThread();
    }
  }

  @Override
  public void closeAutoReplyThread() {


//    if (GlobalData.autoReplyThread != null) {
//      GlobalData.autoReplyThread.stop = true;
//      GlobalData.autoReplyThread.interrupt();
//      GlobalData.autoReplyThread = null;
//    }
  }

  @Override
  public void closeSendBarrageThread() {
    BarrageHandler.setClose(true);
  }

  @Override
  public void closeGiftShieldThread() {

    if (GlobalData.giftShieldThread != null
            && !GlobalData.giftShieldThread.getState().toString().equals("TERMINATED")) {
      GlobalData.giftShieldThread.FLAG = false;
      GlobalData.giftShieldThread.interrupt();
    }
  }

  @Override
  public void closeFollowShieldThread() {

    if (GlobalData.followShieldThread != null
            && !GlobalData.followShieldThread.getState().toString().equals("TERMINATED")) {
      GlobalData.followShieldThread.FLAG = false;
      GlobalData.followShieldThread.interrupt();
    }
  }

  @Override
  public void closeWelcomeShieldThread() {
    if (GlobalData.welcomeShieldThread != null
            && !GlobalData.welcomeShieldThread.getState().toString().equals("TERMINATED")) {
      GlobalData.welcomeShieldThread.FLAG = false;
      GlobalData.welcomeShieldThread.interrupt();
    }
  }

  @Override
  public void closeSmallHeartThread() {

    if (GlobalData.smallHeartThread != null) {
      GlobalData.smallHeartThread.FLAG = true;
      GlobalData.smallHeartThread.interrupt();
      GlobalData.smallHeartThread = null;
    }
  }

}
